//
//  Album.m
//  UI-05 Homework UIScrollView
//
//  Created by 冀永金 on 16/7/20.
//  Copyright © 2016年 冀永金. All rights reserved.
//

#import "Album.h"
#import "MyScrollView.h"
#define WIDTH self.frame.size.width
#define HEIGHT self.frame.size.height
@interface Album ()<UIScrollViewDelegate>
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIPageControl *page;
@property (nonatomic, strong) MyScrollView *smallOfScrollView;
@end
@implementation Album
- (instancetype)initWithFrame:(CGRect)frame images:(NSArray<UIImage *> *)images {
    self = [super initWithFrame:frame];
    if (self) {
        
        [self createView];
        [self createScroll:images];
    }
    return self;
}
- (void)createScroll:(NSArray *)images
{
    
    self.scrollView.contentSize = CGSizeMake((images.count + 2) * WIDTH, 0);
    // 前后各加一个小scrollView
    MyScrollView *firstView = [[MyScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) images:images.lastObject];
    [self.scrollView addSubview:firstView];
    for (NSInteger i = 0; i < images.count; i++) {
       MyScrollView *scroll = [[MyScrollView alloc] initWithFrame:CGRectMake(WIDTH * (i + 1), 0, WIDTH, HEIGHT) images:images[i]];
        
        [self.scrollView addSubview:scroll];
        
    }
    MyScrollView *lastView = [[MyScrollView alloc] initWithFrame:CGRectMake(12 * WIDTH, 0, WIDTH, HEIGHT) images:images.firstObject];
    [self.scrollView addSubview:lastView];
    self.page.numberOfPages = images.count - 2;
}
- (void)createView {
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    [self addSubview:self.scrollView];
    self.scrollView.pagingEnabled = YES;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.delegate = self;
    self.scrollView.contentOffset = CGPointMake(WIDTH , 0);
   self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(100, HEIGHT - 50, 200, 50)];
    [self addSubview:self.page];
    [self addSubview:_page];
    [_page addTarget:self action:@selector(handleMove:) forControlEvents:UIControlEventValueChanged];
    
}
- (void)handleMove:(UIPageControl *)page {
    [self.scrollView setContentOffset:CGPointMake(self.frame.size.width * (page.currentPage + 1 ), 0) animated:YES];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (self.scrollView.contentOffset.x < self.frame.size.width) {
        scrollView.contentOffset = CGPointMake(self.frame.size.width * self.page.numberOfPages, 0);
    }else if (self.scrollView.contentOffset.x > self.frame.size.width * self.page.numberOfPages) {
        scrollView.contentOffset = CGPointMake(self.frame.size.width, 0);
    }
    self.page.currentPage = (scrollView.contentOffset.x - self.frame.size.width) / self.frame.size.width;
    // 完成缩放
    for (UIView *view in self.scrollView.subviews) {
        
        if ([view isKindOfClass:[MyScrollView class]]) {
            
          MyScrollView *scrollV = (MyScrollView *)view;
            scrollV.zoomScale = 1.0;
        }
        
    }
    

}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (v0oid)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
